package com.chushouya.manager.service.api.impl;

import com.chushouya.common.support.Contexts;
import com.chushouya.common.support.RequestHeaderContext;
import com.chushouya.manager.dto.admin.log.LoginLogDTO;
import com.chushouya.manager.dto.admin.login.WebAppLoginUser;
import com.chushouya.manager.service.admin.LoginLogService;
import com.chushouya.manager.service.api.LoginService;
import com.chushouya.manager.service.api.WebAppTokenService;
import com.general.framework.core.constant.Constants;
import com.general.framework.core.enums.BusinessStatus;
import com.general.framework.core.enums.ContextUserType;
import com.general.framework.core.lang.Dates;
import com.general.framework.core.lang.Lists;
import com.general.framework.core.lang.Strings;
import com.general.framework.web.utils.IpAddressUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
@Slf4j
public class LoginServiceImpl implements LoginService {

    @Resource
    private WebAppTokenService webAppTokenService;


    @Resource
    private LoginLogService loginLogService;

    @Override
    public void logout(String token) {
        webAppTokenService.delLoginToken(token);
    }

    /**
     * 客户端设备信息
     */
    private void setClientInfo(WebAppLoginUser loginUser) {
        final RequestHeaderContext headerContext = Contexts.getHeaderContext();
        loginUser.setIpAddr(headerContext.getIp());
        loginUser.setOsType(Strings.defaultString(headerContext.getOsType(), "Unknown"));
        loginUser.setOsVersion(Strings.defaultString(headerContext.getOsVersion(), "Unknown"));
        loginUser.setClientType(Strings.defaultString(headerContext.getPlatform(), "Unknown"));
        loginUser.setAppVersion(Strings.defaultString(headerContext.getAppVersion(), "Unknown"));
        loginUser.setDeviceId(Strings.defaultString(headerContext.getDeviceId(), "Unknown"));
        loginUser.setLoginLocation(IpAddressUtil.getAddress(headerContext.getIp()));
    }

    public void saveLoginLog(ContextUserType userType, String userName, String status, String message) {
        final RequestHeaderContext header = Contexts.getHeaderContext();
        LoginLogDTO loginLog = new LoginLogDTO();
        loginLog.setUserType(userType.value());
        loginLog.setUserName(userName);

        loginLog.setLoginIp(header.getIp());
        loginLog.setLoginLocation(IpAddressUtil.getAddress(header.getIp()));
        List<String> loginDetailList = Lists.newArrayList();
        if (Strings.isNotEmpty(header.getOsType())) {
            loginDetailList.add("osType:".concat(header.getOsType()));
        }
        if (Strings.isNotEmpty(header.getOsVersion())) {
            loginDetailList.add("osVersion:".concat(header.getOsVersion()));
        }
        if (Strings.isNotEmpty(header.getPlatform())) {
            loginDetailList.add("platform:".concat(header.getPlatform()));
        }
        if (Strings.isNotEmpty(header.getAppVersion())) {
            loginDetailList.add("appVersion:".concat(header.getAppVersion()));
        }
        if (Strings.isNotEmpty(header.getDeviceId())) {
            loginDetailList.add("deviceId:".concat(header.getDeviceId()));
        }
        loginLog.setLoginDetail(Strings.join(loginDetailList, ","));
        if (Strings.equalsAny(status, Constants.LOGIN_SUCCESS, Constants.REGISTER, Constants.LOGOUT)) {
            loginLog.setStatus(BusinessStatus.SUCCESS.getCode());
        } else if (Constants.LOGIN_FAIL.equals(status)) {
            loginLog.setStatus(BusinessStatus.FAIL.getCode());
        }
        loginLog.setMsg(message);
        loginLog.setLoginTime(Dates.getTimeNow());

        loginLogService.saveLoginLog(loginLog);
    }

}
